---
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT -->
title: Common Properties & Callbacks
description: Common Properties & Callbacks
---

import SlintProperty  from '/src/components/SlintProperty.astro';
import CodeSnippetMD from '/src/components/CodeSnippetMD.astro';
import Link from '/src/components/Link.astro';

The Slint elements have many common properties, callbacks and behavior.
This page describes these properties and their usage.


## Common Visual Properties

These properties are valid on all visual items. For example `Rectangle`, `Text`, and `layouts`. Non
visual items such as `Timer` don't have these properties.


### x, y
<SlintProperty propName="x, y" typeName="length" >
The position of the element relative to its parent.
</SlintProperty>

### z
<SlintProperty propName="z" typeName="float" >
Allows to specify a different order to stack the items with its siblings.
The value must be a compile time constant.

:::note[Note]
Currently the `z` value is a compile time constant and cannot be changed at runtime.
:::

</SlintProperty>


### width, height
<SlintProperty propName="width, height" typeName="length" >
The width and height of the element. When set, this overrides the default size.
</SlintProperty>


### opacity
<CodeSnippetMD imagePath="/src/assets/generated/rectangle-opacity.png" scale="3" imageWidth="100" imageHeight="310"  imageAlt='rectangle opacity'>
```slint playground
component ImageInfo inherits Rectangle {
    in property <float> img-opacity: 1.0;
    background: transparent;
    VerticalLayout {
        spacing: 5px;
        Image {
            source: @image-url("elements/slint-logo.png");
            opacity: img-opacity;
        }
        Text {
            text: "opacity: " + img-opacity;
            color: white;
            horizontal-alignment: center;
        }
    }
}
export component Example inherits Window {
    width: 100px;
    height: 310px;
    background: transparent;
    Rectangle {
        background: #141414df;
        border-radius: 10px;
    }
    VerticalLayout {
        spacing: 15px;
        padding-top: 10px;
        padding-bottom: 10px;
        ImageInfo {
            img-opacity: 1.0;
        }
        ImageInfo {
            img-opacity: 0.6;
        }
        ImageInfo {
            img-opacity: 0.3;
        }
    }
}
```
</CodeSnippetMD>

<SlintProperty propName="opacity" typeName="float" defaultValue="1">
A value between 0 and 1 (or a percentage) that is used to draw
the element and its children with transparency.
0 is fully transparent (invisible), and 1 is fully opaque.
The opacity is applied to the tree of child elements as if they
were first drawn into an intermediate layer, and then the whole layer is rendered with this opacity.

The following example demonstrates the opacity property with children. Note the software renderer does
not support layer opacity and this will result in a different end result as shown.

<CodeSnippetMD noScreenShot imagePath="/src/assets/generated/layer-opacity.png" imageWidth="150" imageHeight="390"  imageAlt='layer opacity'>
```slint playground
 Rectangle {
        opacity: 50%;
        Rectangle {
            x: 0;
            y: 0;
            width: 100px;
            height: 100px;
            background: blue;
        }

        Rectangle {
            x: 50px;
            y: 50px;
            width: 100px;
            height: 100px;
            background: green;
        }
    }
```
</CodeSnippetMD>


<CodeSnippetMD skip="true" imagePath="/src/assets/generated/layer-opacity.png" scale="3" imageWidth="150" imageHeight="390"  imageAlt='layer opacity'>
```slint playground
export component CheckerBoardRow inherits HorizontalLayout {
    property <length> size: 15px;
    property <int> columns: root.width / size;
    in property <bool> isOdd: true;
    property <int> isModOdd: isOdd ? 1 : 0;

    height: size;
    width: 200px;
    for i in columns : Rectangle {
        width: size;
        height: size;
        background: i.mod(2) == isModOdd ? #262626 : #2c2c2e;
    }
}

export component CheckerBoard inherits VerticalLayout {
    in property <length> size: 15px;
    property <int> rows: root.height / size;

    width: 200px;
    height: 400px;
    for i in rows : CheckerBoardRow {
        isOdd: i.mod(2) != 0;

    }
}
export component ExportedComponent2 inherits Window {
    width: 150px;
    height: 410px;
    background: transparent;
    CheckerBoard {
        width: parent.width;
        height: parent.height;
    }

    VerticalLayout {
        spacing: 15px;
        padding-top: 10px;
        padding-bottom: 10px;

        VerticalLayout {
            spacing: 5px;
            Rectangle {
                width: 100px;
                height: 150px;
                opacity: 50%;
                Rectangle {
                    x: 0;
                    y: 0;
                    width: 100px;
                    height: 100px;
                    background: blue;
                }

                Rectangle {
                    x: 50px;
                    y: 50px;
                    width: 100px;
                    height: 100px;
                    background: green;
                }
            }

            Text {
                text: "With Layer Opacity";
                color: white;
                horizontal-alignment: center;
            }
        }

        VerticalLayout {
            spacing: 5px;
            Rectangle {
                width: 100px;
                height: 150px;
                Rectangle {
                    x: 0;
                    y: 0;
                    width: 100px;
                    height: 100px;
                    background: blue;
                    opacity: 50%;
                }

                Rectangle {
                    x: 50px;
                    y: 50px;
                    width: 100px;
                    height: 100px;
                    background: green;
                    opacity: 50%;
                }
            }

            Text {
                text: "Without Layer Opacity\n(Software Renderer Only)";
                color: white;
                horizontal-alignment: center;
            }
        }
    }
}
```
</CodeSnippetMD>

</SlintProperty>

### visible
<SlintProperty propName="visible" typeName="bool" defaultValue="true">
When set to `false`, the element and all his children won't be drawn and not react to mouse input. The element
will still take up layout space within any layout container.

</SlintProperty>

### absolute-position
<SlintProperty propName="absolute-position" typeName="struct" structName="Point" propertyVisibility="out">

A common issue is that in a UI with many nested components it's useful to know their (x,y)position relative to
the main window or screen. This convenience property gives easy read only access to that value.

It represents a point specifying the absolute position within the enclosing <Link type="Window"/> or <Link type="PopupWindow"/>.
It defines coordinates (x,y) relative to the enclosing Window or PopupWindow, but the reference frame is unspecified
(could be screen, window, or popup coordinates).
</SlintProperty>



## Miscellaneous

### cache-rendering-hint
<SlintProperty typeName="bool" propName="cache-rendering-hint" default="false" >
When set to `true`, this provides a hint to the renderer to cache the contents of the element
and all the children into an intermediate cached layer. For complex sub-trees that rarely
change this may speed up the rendering, at the expense of increased memory consumption. Not
all rendering backends support this, so this is merely a hint.
</SlintProperty>



### dialog-button-role
<SlintProperty typeName="enum" enumName="DialogButtonRole" defaultValue="none">
Specify that this is a button in a `Dialog`.
</SlintProperty>

## Common Callbacks

### `init()`

Every element implicitly declares an `init` callback. You can assign a code block to it that will be invoked when the
element is instantiated and after all properties are initialized with the value of their final binding. The order of
invocation is from inside to outside. The following example will print "first", then "second", and then "third":

```slint
component MyButton inherits Rectangle {
    in-out property <string> text: "Initial";
    init => {
        // If `text` is queried here, it will have the value "Hello".
        debug("first");
    }
}

component MyCheckBox inherits Rectangle {
    init => { debug("second"); }
}

export component MyWindow inherits Window {
    MyButton {
        text: "Hello";
        init => { debug("third"); }
    }
    MyCheckBox {
    }
}
```

Don't use this callback to initialize properties, because this violates the declarative principle.

Even though the `init` callback exists on all components, it cannot be set from application code,
i.e. an `on_init` function does not exist in the generated code. This is because the callback is invoked during the creation of the component, before you could call `on_init` to actually set it.

While the `init` callback can invoke other callbacks, e.g. one defined in a `global` section, and
you _can_ bind these in the backend, this doesn't work for statically-created components, including
the window itself, because you need an instance to set the globals binding. But it is possible
to use this for dynamically created components (for example ones behind an `if`):

```slint
export global SystemService  {
    // This callback can be implemented in native code using the Slint API
    callback ensure_service_running();
}

component MySystemButton inherits Rectangle {
    init => {
        SystemService.ensure_service_running();
    }
    // ...
}

export component AppWindow inherits Window {
    in property <bool> show-button: false;

    // MySystemButton isn't initialized at first, only when show-button is set to true.
    // At that point, its init callback will call ensure_service_running()
    if show-button : MySystemButton {}
}
```

## Accessibility Properties

Use the following `accessible-` properties to make your items interact well with software like screen readers, braille terminals and other software to make your application accessible.
`accessible-role` must be set in order to be able to set any other accessible property or callback.

### accessible-role
<SlintProperty typeName="enum" enumName="AccessibleRole">
The role of the element. This property is mandatory to be able to use any other accessible properties. It should be set to a constant value. (default value: `none` for most elements, but `text` for the Text element)
</SlintProperty>

### accessible-checkable
<SlintProperty typeName="bool" propName="accessible-checkable" default="false">
Whether the element is can be checked or not.
</SlintProperty>

### accessible-checked
<SlintProperty typeName="bool" propName="accessible-checked" default="false">
Whether the element is checked or not. This maps to the "checked" state of checkboxes, radio buttons, and other widgets.
</SlintProperty>

### accessible-description
<SlintProperty typeName="string" propName="accessible-description" default='""'>
The description for the current element.
</SlintProperty>

### accessible-enabled
<SlintProperty typeName="bool" propName="accessible-enabled" default="true">
Whether the element is enabled or not. This maps to the "enabled" state of most widgets. (default value: `true`)
</SlintProperty>

### accessible-expandable
<SlintProperty typeName="bool" propName="accessible-expandable" default="false">
Whether the element can be expanded or not. For example, a `ComboBox` widget should set this to true,
as its selection can be changed via an expandable popup.
</SlintProperty>

### accessible-expanded
<SlintProperty typeName="bool" propName="accessible-expanded" default="false">
Whether the element is expanded or not. Applies to combo boxes, menu items,
tree view items and other widgets.
</SlintProperty>

### accessible-label
<SlintProperty typeName="string" propName="accessible-label" default='""'>
The label for an interactive element. (default value: empty for most elements, or the value of the `text` property for Text elements)
</SlintProperty>

### accessible-value-maximum
<SlintProperty typeName="float" propName="accessible-value-maximum" default="0">
The maximum value of the item. This is used for example by spin boxes.
</SlintProperty>

### accessible-value-minimum
<SlintProperty typeName="float" propName="accessible-value-minimum" default="0">
The minimum value of the item.
</SlintProperty>

### accessible-value-step
<SlintProperty typeName="float" propName="accessible-value-step" default="0">
The smallest increment or decrement by which the current value can change. This corresponds to the step by which a handle on a slider can be dragged.
</SlintProperty>

### accessible-value
<SlintProperty typeName="string" propName="accessible-value" default='""'>
The current value of the item.
</SlintProperty>

### accessible-placeholder-text
<SlintProperty typeName="string" propName="accessible-placeholder-text" default='""'>
A placeholder text to use when the item's value is empty. Applies to text elements.
</SlintProperty>

### accessible-read-only
<SlintProperty typeName="bool" propName="accessible-read-only" default="false">
Whether the element's content can be edited. This maps to the "read-only" state of line edit and text edit widgets.
</SlintProperty>

### accessible-item-selectable
<SlintProperty typeName="bool" propName="accessible-value" >
Whether the element can be selected or not.
</SlintProperty>

### accessible-item-selected
<SlintProperty typeName="bool" propName="accessible-item-selected" >
Whether the element is selected or not. This maps to the "is-selected" state of listview items.
</SlintProperty>

### accessible-item-index
<SlintProperty typeName="int" propName="accessible-item-index">
 The index (starting from 0) of this element in a group of similar elements. Applies to list items, radio buttons and other elements.
</SlintProperty>

### accessible-item-count
<SlintProperty typeName="int" propName="accessible-item-count">
The total number of elements in a group. Applies to the parent container of a group of element such as list views, radio button groups or other grouping elements.
</SlintProperty>


## Accessibility Callbacks
You can also use the following callbacks that are going to be called by the accessibility framework:

### accessible-action-default()
Invoked when the default action for this widget is requested (eg: pressed for a button).

### accessible-action-set-value(string)
Invoked when the user wants to change the accessible value.

### accessible-action-increment()
Invoked when the user requests to increment the value.

### accessible-action-decrement()
Invoked when the user requests to decrement the value.

### accessible-action-expand()
Invoked when the user requests to expand the widget (eg: disclose the list of available choices for a combo box).
